package api

import (
	"log"

	"gitee.com/dingiyan/goMyAdmin/service/interfaces"
	"github.com/gin-gonic/gin"
)

// 获取表字段列表
func GetTableFileds(c *gin.Context) {
	var body struct {
		DsId         uint   `json:"dsId"`
		DatabaseName string `json:"databaseName"`
		TableName    string `json:"tableName"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		c.JSON(200, resError(1, "参数错误"+err.Error()))
		return
	}
	dbm, err := getDBManager(getUserId(c), body.DsId, body.DatabaseName)
	if err != nil {
		log.Println(err)
		c.JSON(200, resError(2, "获取数据库管理对象出错！"+err.Error()))
		return
	}

	fields, err := dbm.GetFields(body.DatabaseName, body.TableName)

	if err != nil {
		log.Println(err)
		c.JSON(200, resError(3, "获取表字段出错！"+err.Error()))
		return
	}

	c.JSON(200, success(fields))

}

// 添加一个字段信息
func AddTableFileds(c *gin.Context) {
	var body struct {
		DsId         uint                    `json:"dsId"`
		DatabaseName string                  `json:"databaseName"`
		TableName    string                  `json:"tableName"`
		Field        interfaces.FieldOptions `json:"field"`
		AfterField   string                  `json:"afterField"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		c.JSON(200, resError(1, "参数错误"+err.Error()))
		return
	}
	dbm, err := getDBManager(getUserId(c), body.DsId, body.DatabaseName)
	if err != nil {
		log.Println(err)
		c.JSON(200, resError(2, "获取数据库管理对象出错！"+err.Error()))
		return
	}

	var opts = map[string]string{
		"afterField": body.AfterField,
	}
	err = dbm.AddField(body.TableName, body.Field, opts)

	if err != nil {
		log.Println(err)
		c.JSON(200, resError(3, "添加表字段出错！"+err.Error()))
		return
	}

	c.JSON(200, success(true))

}

// 修改一个字段信息
func UpdateTableFileds(c *gin.Context) {
	var body struct {
		DsId         uint                    `json:"dsId"`
		DatabaseName string                  `json:"databaseName"`
		TableName    string                  `json:"tableName"`
		OldFieldName string                  `json:"oldFieldName"`
		Field        interfaces.FieldOptions `json:"field"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		c.JSON(200, resError(1, "参数错误"+err.Error()))
		return
	}
	dbm, err := getDBManager(getUserId(c), body.DsId, body.DatabaseName)
	if err != nil {
		log.Println(err)
		c.JSON(200, resError(2, "获取数据库管理对象出错！"+err.Error()))
		return
	}

	err = dbm.UpdateField(body.TableName, body.OldFieldName, body.Field)

	if err != nil {
		log.Println(err)
		c.JSON(200, resError(3, "修改表字段出错！"+err.Error()))
		return
	}

	c.JSON(200, success(true))

}

// 删除一个字段
func DropTableFileds(c *gin.Context) {
	var body struct {
		DsId         uint   `json:"dsId"`
		DatabaseName string `json:"databaseName"`
		TableName    string `json:"tableName"`
		FieldName    string `json:"fieldName"`
	}
	if err := c.ShouldBindJSON(&body); err != nil {
		c.JSON(200, resError(1, "参数错误"+err.Error()))
		return
	}
	dbm, err := getDBManager(getUserId(c), body.DsId, body.DatabaseName)
	if err != nil {
		log.Println(err)
		c.JSON(200, resError(2, "获取数据库管理对象出错！"+err.Error()))
		return
	}

	err = dbm.DeleteField(body.TableName, body.FieldName)

	if err != nil {
		log.Println(err)
		c.JSON(200, resError(3, "删除表字段出错！"+err.Error()))
		return
	}

	c.JSON(200, success(true))

}

// 执行原始sql语句接口
func ExecuteSql(c *gin.Context) {
	var body struct {
		DsId         uint   `json:"dsId"`
		DatabaseName string `json:"databaseName"`
		Sql          string `json:"sql"`
	}

	if err := c.ShouldBindJSON(&body); err != nil {
		log.Println(err)
		c.JSON(200, resError(1, "参数错误"))
		return
	}
	if body.Sql == "" {
		c.JSON(200, resError(1, "sql不能为空"))
		return
	}
	dbm, err := getDBManager(getUserId(c), body.DsId, body.DatabaseName)
	if err != nil {
		log.Println(err)
		c.JSON(200, resError(2, "获取数据库管理对象出错！"))
		return
	}

	res, affectedRows, err := dbm.ExecRawSQL(body.Sql)
	if err != nil {
		log.Println(err)
		c.JSON(200, resError(3, "执行sql出错"+err.Error()))
		return
	}
	c.JSON(200, success(gin.H{
		"result":       res,
		"affectedRows": affectedRows,
	}))
}
